package com.delin.opengltest;

import android.opengl.GLSurfaceView;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

/**
 * Created by Administrator on 2016/3/8 0008.
 */
public class MyRenderer implements GLSurfaceView.Renderer {

    float[] triangleData = new float[]{
            0.1f, 0.6f, 0.0f,
            -0.3f, 0.0f, 0.0f,
            0.3f, 0.1f, 0.0f
    };
    int [] triangleColor = new int[]{
            65535,0,0,0,
            0,65535,0,0,
            0,0,65535,0,

    };
    float[] rectData = new float[]{
            0.4f, 0.4f, 0.0f,
            0.4f, -0.4f, 0.0f,
            -0.4f, 0.4f, 0.0f,
            -0.4f,-0.4f,0.0f
    };
    int [] rectColor = new int[]{
            0,65535,0,0,
            0,0,65535,0,
            65535,0,0,0,
            65535,65535,0,0,
    };
    float[] rect2Data = new float[]{
            -0.4f, 0.4f, 0.0f,
            0.4f, 0.4f, 0.0f,
            0.4f,-0.4f, 0.0f,
            -0.4f,-0.4f,0.0f
    };
    float [] pentacle = new float[]{
            0.4f, 0.4f, 0.0f,
            0.2f, 0.3f, 0.0f,
            0.5f,0.0f, 0f,
            -0.4f,0.0f,0f,
            -0.1f,-0.3f,0f
    };
    FloatBuffer triangleBuffer;
    IntBuffer triangleColorBuffer;
    FloatBuffer rectDataBuffer;
    IntBuffer rectColorBuffer;
    FloatBuffer rect2DataBuffer;
    FloatBuffer pentacleBuffer;


    public MyRenderer() {
        triangleBuffer = floatBufferUtil(triangleData);
        triangleColorBuffer = intBufferUtil(triangleColor);
        rectDataBuffer = floatBufferUtil(rectData);
        rectColorBuffer = intBufferUtil(rectColor);
        rect2DataBuffer = floatBufferUtil(rect2Data);
        pentacleBuffer = floatBufferUtil(pentacle);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        //当GLSurfaceView被创建时回调该方法
        gl10.glDisable(GL10.GL_DITHER);//关闭抗抖动
        gl10.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);//设置系统对透视进行修正
        gl10.glClearColor(0, 0, 0, 0);
        gl10.glShadeModel(GL10.GL_SMOOTH);//设置阴影平滑模式
        gl10.glEnable(GL10.GL_DEPTH_TEST);//启用深度测试
        gl10.glDepthFunc(GL10.GL_LEQUAL);//设置深度测试的类型

    }

    @Override
    public void onSurfaceChanged(GL10 gl, int i, int i1) {
        //当GLSurfaceView的大小改变时回调该方法
        //设置3D视窗的大小及位置前两个参数为指定该视窗的位置，后两个为宽，高
        gl.glViewport(0, 0, i, i1);
        //将当前矩阵模式设为投影矩阵
        gl.glMatrixMode(GL10.GL_PROJECTION);
        //初始化单位矩阵
        gl.glLoadIdentity();
        //计算透视视窗的高宽比
        float ratio = (float) i / i1;
        //调用此方法设置透视视窗的控件大小
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        //Renderer对象调用该方法绘制GLSurfaceView
        //清除屏幕缓存和深度缓存
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        //启用顶点坐标数据
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        //启用顶点颜色数据
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
        //设置当前矩阵堆栈为模型堆栈
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        //重置当前的模型视图矩阵
        gl.glLoadIdentity();
        gl.glTranslatef(-0.32f, 0.35f, -1.5f);
        //设置顶点的位置数据
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuffer);
        //设置顶点的颜色数据
        gl.glColorPointer(4,GL10.GL_FIXED,0,triangleColorBuffer);
        //根据顶点数据绘制平面图形
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,3);

        //重置当前的模型视图矩阵
        gl.glLoadIdentity();
        gl.glTranslatef(0.6f, 0.8f, -1.5f);
        //设置顶点的位置数据
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, rectDataBuffer);
        //设置顶点的颜色数据
        gl.glColorPointer(4,GL10.GL_FIXED,0,rectColorBuffer);
        //根据顶点数据绘制平面图形
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4);

        //重置当前的模型视图矩阵
        gl.glLoadIdentity();
        gl.glTranslatef(-0.4F, -0.5f, -1.5f);
        //设置顶点的位置数据
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0, rect2DataBuffer);

        //根据顶点数据绘制平面图形
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,4);

        //重置当前的模型视图矩阵
        gl.glLoadIdentity();
        gl.glTranslatef(0.4f, -0.5f, -1.5f);
        gl.glColor4f(1.0f,0.2f,0.2f,0.0f);
        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
        //设置顶点的位置数据
        gl.glVertexPointer(3, GL10.GL_FLOAT, 0,pentacleBuffer);

        //根据顶点数据绘制平面图形
        gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP,0,5);

        gl.glFinish();
        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    }

    private FloatBuffer floatBufferUtil(float[] arr){
        FloatBuffer floatBuffer;
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(arr.length*4);
        //数组排列用nativeOrder（）
        byteBuffer.order(ByteOrder.nativeOrder());
        floatBuffer = byteBuffer.asFloatBuffer();
        floatBuffer.put(arr);
        floatBuffer.position(0);

        return floatBuffer;
    }

    private IntBuffer intBufferUtil(int[] arr){
        IntBuffer intBuffer;
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(arr.length*4);
        //数组排列用nativeOrder（）
        byteBuffer.order(ByteOrder.nativeOrder());
        intBuffer = byteBuffer.asIntBuffer();
        intBuffer.put(arr);
        intBuffer.position(0);
        return intBuffer;
    }
}
